package cn.stronglink.assetmanage.business.search.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;
import java.util.Vector;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.business.search.DetailsUtils;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.listener.onRecyclerListener;

public class SearchAdapter extends RecyclerView.Adapter<SearchAdapter.ViewHolder> {
    private Vector<Boolean> mVector = new Vector<>();
    private Context mContext;
    private List<SearchAsset> datas;
    private onRecyclerListener listener;
    private Bitmap bitmap;

    public SearchAdapter(Context mContext) {
        this.mContext = mContext;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_search_gray, viewGroup, false);
        return new ViewHolder(view, listener);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SearchAsset searchAsset = datas.get(position);
        holder.bindRepo(searchAsset, position);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }


    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(onRecyclerListener listener) {
        this.listener = listener;
    }

    public void notifyDatasChanged(List<SearchAsset> data) {
        if (this.datas != null) {
            this.datas.clear();
        }
        this.datas = data;
        notifyDataSetChanged();
    }


    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private List<SearchAsset> data;
        private TextView tv_asset_name, tv_status, tv_asset_no, tv_rfid_no, tv_sn_no, tv_room, tv_user;
        private onRecyclerListener listener;
        private ImageView iv_check_picture;
        private LinearLayout ll_layout;


        public ViewHolder(View itemView, onRecyclerListener listener) {
            super(itemView);
            ll_layout = itemView.findViewById(R.id.ll_layout);
            tv_asset_name = itemView.findViewById(R.id.tv_asset_name);
            tv_asset_no = itemView.findViewById(R.id.tv_asset_no);
            tv_status = itemView.findViewById(R.id.tv_status);
            tv_rfid_no = itemView.findViewById(R.id.tv_rfid_no);
            tv_sn_no = itemView.findViewById(R.id.tv_sn_no);
            tv_room = itemView.findViewById(R.id.tv_room);
            tv_user = itemView.findViewById(R.id.tv_user);
            iv_check_picture = itemView.findViewById(R.id.iv_check_picture);
            this.listener = listener;
            itemView.setOnClickListener(this);
        }


        public void bindRepo(SearchAsset searchAsset, int position) {
            if (position % 2 == 0) {
                ll_layout.setBackgroundResource(R.color.colorWhite);
            }
            if (searchAsset != null) {
                if (searchAsset.getAssetNo() != null) {
                    tv_asset_no.setText(searchAsset.getAssetNo());
                    tv_asset_no.setVisibility(View.VISIBLE);
                } else {
                    tv_asset_no.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getAssetName() != null) {
                    tv_asset_name.setText(searchAsset.getAssetName());
                    tv_asset_name.setVisibility(View.VISIBLE);
                } else {
                    tv_asset_name.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getRfidNo() != null) {
                    tv_rfid_no.setText(searchAsset.getRfidNo());
                    tv_rfid_no.setVisibility(View.VISIBLE);
                } else {
                    tv_rfid_no.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getSerialNo() != null) {
                    tv_sn_no.setText(searchAsset.getSerialNo());
                    tv_sn_no.setVisibility(View.VISIBLE);
                } else {
                    tv_sn_no.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getRoomName() != null) {
                    tv_room.setText(searchAsset.getRoomName());
                    tv_room.setVisibility(View.VISIBLE);
                } else {
                    tv_room.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getEndUserName() != null) {
                    tv_user.setText(searchAsset.getEndUserName());
                    tv_user.setVisibility(View.VISIBLE);
                } else {
                    tv_user.setVisibility(View.INVISIBLE);
                }
                if (searchAsset.getPictureUri() != null) {
                    bitmap = DetailsUtils.getBitmap(searchAsset.getPictureUri());
                    if (bitmap != null) {
                        iv_check_picture.setImageBitmap(bitmap);
                        iv_check_picture.setVisibility(View.VISIBLE);
                    } else {
                        iv_check_picture.setVisibility(View.INVISIBLE);
                    }
                }
                tv_status.setVisibility(View.VISIBLE);
                switch (searchAsset.getStatus()) {
                    case 1:
                        tv_status.setText("在库");
                        break;
                    case 2:
                        tv_status.setText("已上架");
                        break;
                    case 3:
                        tv_status.setText("报废");
                        break;
                    case 4:
                        tv_status.setText("已出库");
                        break;
                    case 5:
                        tv_status.setText("待验收");
                        break;
                    case 6:
                        tv_status.setText("在使用");
                        break;
                    default:
                        break;

                }

            }
        }

        @Override
        public void onClick(View v) {
            if (listener != null) {
                listener.onItemClicked(v, getAdapterPosition());
            }
        }
    }
}
